package com.uum.mq.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uum.common.core.result.Result;
import com.uum.mq.pojo.entity.SysMqMsg;
import com.uum.mq.pojo.entity.SysUserMsg;
import com.uum.mq.service.SysMqMsgService;
import com.uum.mq.service.SysUserMsgService;
import com.uum.web.util.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "用户消息接口")
@RestController
@RequestMapping("/api/v1/user/msg")
@RequiredArgsConstructor
public class SysUserMsgController {

    private final SysUserMsgService sysUserMsgService;
    private final SysMqMsgService mqMsgService;

        @ApiOperation(value = "用户未读消息")
        @GetMapping("unread")
        public Result unreadMsg() {
        List<SysMqMsg> unreadList =  sysUserMsgService.unreadMsg();
        return Result.success(unreadList);
    }

    @ApiOperation("添加已读消息")
    @PostMapping("read/{messageId}")
    public Result readMessage(@PathVariable Long messageId) {
        SysUserMsg userMsg = new SysUserMsg();
        userMsg.setUserId(JwtUtils.getUserId());
        userMsg.setMessageId(messageId);
        boolean result = sysUserMsgService.save(userMsg);
        return Result.judge(result);
    }

    @ApiOperation("根据用户ID添加所有已读信息")
    @PostMapping("saveReadMsgByUser")
    public Result saveReadMsgByUser(@RequestParam(value = "userId" ) Long userId) {
        //查询出所有的推送消息
        List<Long> messageIdList = mqMsgService.list().stream().map(SysMqMsg::getId).collect(Collectors.toList());
        List<SysUserMsg> saveList = new ArrayList<>();
        for (Long messageId : messageIdList) {
            SysUserMsg sysUserMsg = new SysUserMsg();
            sysUserMsg.setUserId(userId);
            sysUserMsg.setMessageId(messageId);
            saveList.add(sysUserMsg);
        }
        boolean result = sysUserMsgService.saveBatch(saveList);
        return Result.judge(result);
    }

    @ApiOperation("清空已读消息")
    @DeleteMapping("readAll")
    @Transactional
    public Result readAll(@RequestParam(value = "userId",required = false) Long userId){
        if (userId == null) {
            userId = JwtUtils.getUserId();
        }
        List<Long> allMsg = sysUserMsgService.readAll(userId);
        List<SysUserMsg> save = new ArrayList<>();
        if (!allMsg.isEmpty()) {
            for (Long msgId : allMsg) {
                SysUserMsg userMsg = new SysUserMsg();
                userMsg.setUserId(userId);
                userMsg.setMessageId(msgId);
                save.add(userMsg);
            }
            sysUserMsgService.saveBatch(save, save.size());
        }
        return Result.success();
    }

    @ApiOperation("根据用户ID清空消息")
    @PostMapping("delMsgByUserId")
    public Result delMsgByUserId(@RequestBody List<String> userIdList) {
        boolean remove = sysUserMsgService.remove(new LambdaQueryWrapper<SysUserMsg>().in(SysUserMsg::getUserId, userIdList));
        return Result.judge(remove);
    }
}

